package me.zhulu.dubbo.pagination2;

import java.util.*;

/**
 * 分页列表缺省实现
 * 
 * @author amyson wu
 * 
 */
public class DefaultPagedList implements PagedList {
  public static final DefaultPagedList EMPTY_LIST = new DefaultPagedList();

  private List data = null;

  private int pagesCount = 0;

  private int recordsCount = 0;

  private PageInfo pageInfo = null;

  public DefaultPagedList() {
    this.data = new ArrayList();
  }

  public DefaultPagedList(PageInfo pi, int recordsCount) {
    this(null, pi, recordsCount);
  }

  public DefaultPagedList(List data, PageInfo pi, int recordsCount) {
    this.data = data;
    if (pi == null) {
      throw new IllegalArgumentException("分页信息不能为null!");
    } else if (pi.getPageSize() <= 0) {
      throw new IllegalArgumentException("分页的pageSize必须大于0!");
    }
    
    this.pageInfo = pi;
    setRecordsCount(recordsCount);
  }

  public DefaultPagedList setData(List data) {
    this.data = data;
    return this;
  }

  public PageInfo getPageInfo() {
    return pageInfo;
  }

  public int getPagesCount() {
    return pagesCount;
  }

  public int getRecordsCount() {
    return recordsCount;
  }

  protected int calcPagesCount() {
    if (recordsCount == 0) {
      return 0;
    } else {
      int base = recordsCount / getPageInfo().getPageSize();
      int rest = recordsCount % getPageInfo().getPageSize();
      if (rest > 0)
        return base + 1;
      else
        return base;
    }
  }

  public void setRecordsCount(int recordsCount) {
    this.recordsCount = recordsCount;
    this.pagesCount = calcPagesCount();
  }

  public void add(int index, Object element) {
    data.add(index, element);
  }

  public boolean add(Object o) {
    return data.add(o);
  }

  public boolean addAll(Collection c) {
    return data.addAll(c);
  }

  public boolean addAll(int index, Collection c) {
    return data.addAll(index, c);
  }

  public void clear() {
    data.clear();
  }

  public boolean contains(Object o) {
    return data.contains(o);
  }

  public boolean containsAll(Collection c) {
    return data.containsAll(c);
  }

  public boolean equals(Object o) {
    return data.equals(o);
  }

  public Object get(int index) {
    return data.get(index);
  }

  public int hashCode() {
    return data.hashCode();
  }

  public int indexOf(Object o) {
    return data.indexOf(o);
  }

  public boolean isEmpty() {
    return data.isEmpty();
  }

  public Iterator iterator() {
    return data.iterator();
  }

  public int lastIndexOf(Object o) {
    return data.lastIndexOf(o);
  }

  public ListIterator listIterator() {
    return data.listIterator();
  }

  public ListIterator listIterator(int index) {
    return data.listIterator(index);
  }

  public Object remove(int index) {
    return data.remove(index);
  }

  public boolean remove(Object o) {
    return data.remove(o);
  }

  public boolean removeAll(Collection c) {
    return data.removeAll(c);
  }

  public boolean retainAll(Collection c) {
    return data.retainAll(c);
  }

  public Object set(int index, Object element) {
    return data.set(index, element);
  }

  public int size() {
    return data.size();
  }

  public List subList(int fromIndex, int toIndex) {
    return data.subList(fromIndex, toIndex);
  }

  public Object[] toArray() {
    return data.toArray();
  }

  public Object[] toArray(Object[] a) {
    return data.toArray(a);
  }

    @Override
    public String toString() {
        return "DefaultPagedList{" +
                "data=" + data +
                ", pagesCount=" + pagesCount +
                ", recordsCount=" + recordsCount +
                ", pageInfo=" + pageInfo +
                '}';
    }
}
